package com.qst.rongxiaoserver.controller;

import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.mapper.AddressMapper;
import com.qst.rongxiaoserver.model.AddressModel;
import com.qst.rongxiaoserver.model.dto.AddressDto;
import com.qst.rongxiaoserver.service.IAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   // 请求返回的是json数据格式
@RequestMapping("/api/address")
@Tag(name = "地址管理", description = "地址管理相关接口(服务层实现)")
public class AddressController {

    // 依赖注入 addressService就是  AddressServiceImpl类的某一个实例化对象
    @Autowired
    IAddressService addressService;
    // 地址列表
    @GetMapping("/list")    //  /api/address
    @Operation(summary = "地址列表", description = "地址列表(服务层实现)")
   public Result getAddress(){
        List<AddressModel> listData =  addressService.list();
        return Result.success(listData);   // 返回成功
   }

   // 根据登录用户名查询所有地址
   @GetMapping("/list/name")
   @Operation(summary = "根据登录用户名查询所有地址", description = "根据登录用户名查询所有地址(服务层实现)")
   public Result getAddressByOwnName(String ownName){
        List<AddressModel> listData =  addressService.getAddressListByName(ownName);
        return Result.success(listData);
   }

   // 根据登录用户名查询默认地址
   @GetMapping("/default/name")
   @Operation(summary = "根据登录用户名查询默认地址", description = "根据登录用户名查询默认地址(服务层实现)")
   public Result getDefaultAddressByName(String ownName){
        AddressModel addressModel =  addressService.getDefaultAddressByName(ownName);
        return Result.success(addressModel);
   }
   // 添加地址
    @PostMapping("/add")    // /api/address/add
    @Operation(summary = "添加地址", description = "添加地址(服务层实现)")
    public Result<String> addAddress(@RequestBody @Valid AddressModel addressModel){

//        if(bindingResult.hasErrors()){
//            return Result.fail(bindingResult.getFieldError().getDefaultMessage());
//        }
        // 调用服务层方法
        // 默认添加的地址 非默认地址
        addressModel.setIsDefault(0);
        boolean success =   addressService.save(addressModel);

        return success?  Result.success("添加成功") : Result.fail("添加失败");
    }

    // 设置默认地址
    @PostMapping("/set/default")
    @Operation(summary = "设置默认地址", description = "设置默认地址(服务层实现)")
    public Result<String> setDefaultAddress(@RequestBody AddressDto addressDto){
        boolean isSuccess =  addressService.setDefaultAddress(addressDto.getId(), addressDto.getOwnName());
        return isSuccess?  Result.success("设置成功") : Result.fail("设置失败");

    }
    // 更新某一条数据
    @PostMapping("/update")
    @Operation(summary = "编辑地址", description = "编辑地址(服务层实现)")
    public Result<String> updateAddress(@Valid @RequestBody AddressModel addressModel ){

        boolean success =  addressService.updateById(addressModel);
        return success?  Result.success("更新成功") : Result.fail("更新失败");
    }

    // 根据id删除某一条数据
    @DeleteMapping("/delete")
    @Operation(summary = "根据id删除地址", description = "根据id删除地址(服务层实现)")
    public Result<String> deleteAddressById(@RequestParam int id){
        boolean success =  addressService.removeById(id);
        return success ?  Result.success("删除成功") : Result.fail("删除失败");
    }
}

